#include "network.h"


//好友下线，链表删除好友节点
int delete_friend_offline(struct glob_info *ginfo, char *offline_name)
{
	struct friend_list *pos;
	struct friend_list *tmp;
	while(1)
	{
		tmp = ginfo->list_head;
		list_for_each(tmp, pos)
		{
			if(strcmp(pos->name, offline_name) == 0)
			{	
				break;
			}
		}
		if(pos == NULL)
		{
			show_systems_msg();
			printf("附近的人：%s下线了, 但是他不在好友链表\n", offline_name);
			goto_scanf();
			break;
		}
			
		tmp = ginfo->list_head;
		for(tmp; tmp!=NULL; tmp=tmp->next)
		{
			if(tmp->next == pos)
			{
				break;
			}
		}
		
		tmp->next = pos->next;
		pos->next = NULL;
		free(pos);
		show_systems_msg();  //---------------显示
		printf("好友%s下线了   \n", offline_name);  //下线显示
		goto_scanf();	//---------------显示
		
		break;
	}
	return 0;
}


//好友上线，判断链表中是否存在 或增加该成员
int add_newfriend(struct glob_info *ginfo, struct friend_list *cache_node, char *recv_name)
{
	struct recv_info msg_info;

	ssize_t send_size;

	struct friend_list *new_node;
	struct friend_list *pos;
	struct friend_list *tmp;
	int revalue;
	
	
	while(1)
	{
		list_for_each(ginfo->list_head, pos)
		{
			if(strcmp(pos->name, recv_name) == 0)
			
			break;
		}

		if(pos != NULL)
			break;

		//谁谁谁上线了，插入好友链表
		strcpy(cache_node->name, recv_name);

		new_node = request_friend_info_node(cache_node);

		insert_friend_info_node_to_link_list(ginfo->list_head, new_node);
		
		show_systems_msg();  		//---------------显示
		printf("好友%s上线了 \n" , new_node->name);
		/* printf("好友%s上线了, IP地址为  [%s]\n" ,
				new_node->name, inet_ntoa(((struct sockaddr_in*)&cache_node->addr)->sin_addr)); */
		goto_scanf();				//---------------显示
		
		strcpy(msg_info.name, ginfo->name);//将我们的名字赋值进去
		msg_info.msg_flag = online_flag;//上线标志

		send_size = sendto(ginfo->skt_fd, &msg_info, msg_info.msg_buffer-msg_info.name, 
			0, (struct sockaddr *)&cache_node->addr, sizeof(struct sockaddr_in));//回送对方我们的上线信息
		break;
	}
	return 0;
}

void insert_friend_info_node_to_link_list(struct friend_list *head, struct friend_list *insert_node)
{
	struct friend_list *pos;

	for(pos=head; pos->next != NULL; pos=pos->next);

	pos->next = insert_node;
}


//申请客户端信息链表的头节点
struct friend_list *request_friend_info_node(const struct friend_list *info)
{
	struct friend_list *new_node;

	new_node = malloc(sizeof(struct friend_list));
	if(new_node == NULL)
	{
		show_systems_msg();
		perror("\t申请客户端节点异常");
		goto_scanf();	
		return NULL;
	}

	if(info != NULL)
		*new_node = *info;

	new_node->next = NULL;

	return 	new_node;
}

int recv_fifo_init(struct recv_info *recv_msg, struct friend_list *cache_node)
{
	char buf[32];
	bzero(buf, sizeof(buf));
	
	show_systems_msg();
	printf("\t好友%s需要传输文件 好友IP为  [%s]\n" ,
							recv_msg->name, inet_ntoa(((struct sockaddr_in*)&cache_node->addr)->sin_addr));
	printf("\t约定端口号为%d   文件名字为%s\n", recv_msg->fifo_port, recv_msg->fifo_name);
	goto_scanf();
	
	int skt_fd;
	int retval;
	 
	skt_fd = socket( AF_INET, SOCK_STREAM, 0);
	if(skt_fd == -1)
	{
		show_systems_msg();
		perror("申请套接字失败");
		goto_scanf();
		
		return -1;
	}

	struct sockaddr_in srv_addr;

	srv_addr.sin_family = AF_INET;//指定引用IPV4的协议

	srv_addr.sin_port = htons(recv_msg->fifo_port);//指定端口号，转化为网络字节序（大端序）

	srv_addr.sin_addr.s_addr = inet_addr( inet_ntoa(((struct sockaddr_in*)&cache_node->addr)->sin_addr) );

	retval = connect(skt_fd, (struct sockaddr *)&srv_addr, sizeof(srv_addr));
	
	if(retval == -1)
	{
		perror("客户端连接到服务器失败\n");
	}

	show_systems_msg();
	printf("\t客户端：连接服务器成功\n");
	goto_scanf();
	printf("发送的文件名是：%s\n", recv_msg->fifo_name);
	
	fd2 = open(recv_msg->fifo_name, O_WRONLY | O_CREAT | O_TRUNC, 0777);
		
	pthread_t tid;
	pthread_create(&tid, NULL, recv_fifo_data, &skt_fd);
	
	pthread_join(tid, NULL);
	
	show_systems_msg();
	
	printf("文件 %s 接收完毕\n", recv_msg->fifo_name);
	
	if(recv_msg->fifo_flag == fifo_dir)
	{
		printf("文件夹接收成功\n");
		//DeletS2(s_fifo_name,".zip");
		
		sprintf(buf,"unzip  %s ", recv_msg->fifo_name);
		system(buf);
		sleep(1);
		bzero(buf,sizeof(buf));
		sprintf(buf,"rm %s ", recv_msg->fifo_name);
		system(buf);
	}
	
	goto_scanf();

	return 0;
}


void *recv_fifo_data(void *arg)
{
	int skt_fd = *(int *)arg;
	struct msg_data send_msg, recv_msg;

	ssize_t recv_size;
	ssize_t send_size;
		
	while(1)
	{
		bzero(&recv_msg, sizeof(recv_msg));

		recv_size = recv(skt_fd, &recv_msg, sizeof(recv_msg), 0);
		if(recv_size == -1)
		{
			perror("接受数据异常");
			break;
		}
		if(recv_msg.myfifo.ret == 0)
		{
			printf("文件传输完毕，长度为%llu\n", recv_msg.myfifo.data_len);
			close(fd2);
			break;
		}
		
		write(fd2, recv_msg.myfifo.fifo_buff, recv_msg.myfifo.ret);
		
		send_msg.myfifo.ret = 0;
	    send_msg.myfifo.data_len = send_msg.myfifo.ret + recv_msg.myfifo.data_len;
		
	    send_msg.data_size = sizeof(send_msg);
		
	    send_size = send( skt_fd, &send_msg, send_msg.data_size, 0);
		
		if(send_size == -1)
			printf("发送出错\n");
	
	}
	
	close(skt_fd);
	
	return NULL;
}


int send_fifo_init(struct recv_info *msg, struct sockaddr_in *dest_addr)
{
	int skt_fd;
	int retval;
	ssize_t send_size;
	struct msg_data send_msg;
	move_pp = 19;
	skt_fd = socket( AF_INET, SOCK_STREAM, 0);
	if(skt_fd == -1)
	{
		perror("申请套接字失败");
		return -1;
	}

	struct sockaddr_in srv_addr;

	srv_addr.sin_family = AF_INET;//指定引用IPV4的协议

	srv_addr.sin_port = htons( msg->fifo_port );//指定端口号，转化为网络字节序（大端序）
	srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	
	retval = bind( skt_fd, (struct sockaddr *)&srv_addr, sizeof(srv_addr));
	if(retval == -1)
	{
		perror("绑定套接字地址失败");
	}

	retval = listen(skt_fd, 1);
	if(retval == -1)
	{
		perror("设置最大连接数失败");
	}
	printf("等待连接\n");
	struct sockaddr_in client_addr;
	socklen_t sklen = sizeof(client_addr);
	
	int ckt_fd = accept(skt_fd, (struct sockaddr *)&client_addr, &sklen);	
	
	if(ckt_fd == -1)
	{
		perror("客户端链接失败");
	}
	move_pp = 19;
	printf("连接成功\n");
	
	pthread_t tid;
    pthread_create(&tid, NULL, send_fifo_data, &ckt_fd);
	
	pthread_join(tid, NULL);

	close(ckt_fd);
	close(skt_fd);
	
	show_systems_msg();
	printf("文件发送完毕\n");
	goto_scanf();
	
	return 0;
	
}


void *send_fifo_data(void *arg)
{
	int skt_fd = *(int *)arg;
	struct msg_data send_msg, recv_msg;
	
	ssize_t recv_size;
	ssize_t send_size;
	
	fd1 = open(	s_fifo_name, O_RDONLY);
	
   if(fd1 == -1)
   {
	 printf("open %s failed! : \n", s_fifo_name); //sterror把错误码转化成错误提示
   }
   
   send_msg.myfifo.data_len = 0;
   send_msg.myfifo.ret = read(fd1, send_msg.myfifo.fifo_buff, 1024);
   
   //printf("%d \n", send_msg.myfifo.ret);
   
   send_msg.myfifo.data_len = send_msg.myfifo.ret + send_msg.myfifo.data_len;
   send_msg.data_size = sizeof(send_msg);
   
   send_size = send( skt_fd, &send_msg, send_msg.data_size, 0);
	if(send_size == -1)
		printf("发送出错\n");
	
	while(1)
	{
		
		bzero(&(send_msg.myfifo.fifo_buff), sizeof(send_msg.myfifo.fifo_buff));
		bzero(&recv_msg, sizeof(recv_msg));

		recv_size = recv(skt_fd, &recv_msg, sizeof(recv_msg), 0);
		if(recv_size == -1)
		{
			perror("接受数据异常");
			break;
		}

        //printf("send_fifo_data is %llu Byte\n",recv_msg.myfifo.data_len);
		
		send_msg.myfifo.ret = read(fd1, send_msg.myfifo.fifo_buff, 1024);
		
		if(send_msg.myfifo.ret == 0)
		{
			  printf("copy %llu Byte , read has finshed!\n", send_msg.myfifo.data_len);
			  send_msg.data_size = sizeof(send_msg);
			
			  send_size = send( skt_fd, &send_msg, send_msg.data_size, 0);
			  if(send_size == -1)
				printf("发送出错\n");
			  break;
		}
	    send_msg.myfifo.data_len = send_msg.myfifo.ret + send_msg.myfifo.data_len;
		
	    send_msg.data_size = sizeof(send_msg);
		
	    send_size = send( skt_fd, &send_msg, send_msg.data_size, 0);

		if(send_size == -1)
			printf("发送出错\n");
	
	}
	
	close(fd1);
	return NULL;
}


//自动检索本地网卡的所有信息，发送广播信息到所有网卡（除本地回环网卡）
int broadcast_msg_data(int skt_fd, const void *msg, ssize_t msg_len)
{
	int i;
	struct ifconf ifconf;
	struct ifreq *ifreq;
	struct sockaddr_in dest_addr;
	ssize_t send_size;
	char buf[512];//缓冲区
	//初始化ifconf
	ifconf.ifc_len =512;
	ifconf.ifc_buf = buf;
   
	ioctl(skt_fd, SIOCGIFCONF, &ifconf); //获取所有接口信息

	ifreq = (struct ifreq*)ifconf.ifc_buf;

	for (i=(ifconf.ifc_len/sizeof (struct ifreq)); i>0; i--, ifreq++)
	{
		if(ifreq->ifr_flags == AF_INET)//判断网卡信息是不是IPv4的配置
		{
			//printf("网卡名字叫 [%s]\n" , ifreq->ifr_name);
			
			if(strcmp(ifreq->ifr_name, "lo") == 0)//判断如果是本地回环网卡则不广播数据
				continue;
		
			ioctl(skt_fd, SIOCGIFBRDADDR, ifreq);//通过网卡名字获取广播地址
			
			dest_addr.sin_family = AF_INET;
			dest_addr.sin_port = htons( all_port );
			dest_addr.sin_addr.s_addr = ((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr.s_addr;

			send_size = sendto(skt_fd, msg, msg_len, 
					0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
			if(send_size == -1)
			{
				perror("发送UDP数据失败\n");
				return -1;
			}
		}
    }
    	
	return 0;
}

//接收上线、下线、消息通知
void *recv_broadcast_msg(void *arg)
{
	struct recv_info recv_msg, msg_info;
	struct glob_info *ginfo = arg;
	socklen_t skt_len = sizeof(struct sockaddr_in);

	ssize_t recv_size, send_size;

	struct friend_list *new_node;
	struct friend_list cache_node;
	struct friend_list *pos;
	struct friend_list *tmp;
	int revalue;
	
	while(1)
	{
		bzero(&recv_msg, sizeof(recv_msg));

		recv_size = recvfrom(ginfo->skt_fd, &recv_msg, sizeof(recv_msg), 
			0, (struct sockaddr *)&(cache_node.addr), &skt_len);
		if(recv_size == -1)
		{
			perror("接受UDP数据失败\n");
			break;
		}
		
		switch(recv_msg.msg_flag)
		{
			case online_flag:
			add_newfriend(ginfo, &cache_node, recv_msg.name);
			show_all_friend(ginfo);
			goto_scanf();
				break;
				
			case offline_flag:
				delete_friend_offline(ginfo, recv_msg.name);
				show_all_friend(ginfo);
				goto_scanf();
				break;
				
			case msg_flag:   //消息接收
				show_frient_msg();
				printf("你的%s给你发送消息：%s\n", recv_msg.name, recv_msg.msg_buffer);
				goto_scanf();
				
				break;
				
			case fifo_send:  //文件接收
				revalue = recv_fifo_init(&recv_msg, &cache_node);
				break;		
		}
	}
}


int  chat_with_friend(struct glob_info *ginfo, struct recv_info *msg)
{
	int choose;
	int i = 0;
	char buf[64];
	struct recv_info recv_msg;
	struct sockaddr_in dest_addr;
	struct friend_list *tmp;
	struct friend_list *pos;
	int skt_fd = ginfo->skt_fd;
	ssize_t send_size, msg_len = sizeof(struct recv_info);
	
	show_all_friend(ginfo);
				
	pos = NULL;
	move_status = 0;
	while(1)
	{
		M_ret();
		MOVEDOWN(16); 
		clear_down();
		M_ret();
		MOVEDOWN(16); 
		
		
		printf("请选择好友称呢进行聊天,输入exit退出该聊天\n");
		char name[64];
		
		bzero(name, sizeof(name));
		bzero(s_fifo_name, sizeof(s_fifo_name));
		move_pp = 17;
		
		scanf("%s", name);
		
		if(strcmp(name, "exit") == 0)
		{
			M_ret();
			MOVEDOWN(16); 
			clear_down();
			M_ret();
			MOVEDOWN(16); 
			printf("退出当前聊天\n");
			move_status = 1;
			return 0;
		}
		tmp = ginfo->list_head;
		list_for_each(tmp, pos)
		{
			if(strcmp(pos->name, name) == 0)
				goto conmunti;
		}
		if(pos == NULL)
		{
			M_ret();
			MOVEDOWN(16); 
			clear_down();
			M_ret();
			MOVEDOWN(16); 
			printf("该好友不存在哦\n");
			move_status = 1;
			return 0;
		}
	}	
conmunti:	
	//目标地址
	dest_addr.sin_family = AF_INET;
	dest_addr.sin_port = htons( all_port );

	dest_addr.sin_addr.s_addr = pos->addr.sin_addr.s_addr;

	bzero(msg->msg_buffer, sizeof(msg->msg_buffer));
	
	while(1)
	{
		M_ret();
		MOVEDOWN(16); 
		clear_down();
		M_ret();
		MOVEDOWN(16); 
		printf("请进行选择：\n输入1：开始聊天\n输入2：发送文件\n输入3：退出当前聊天\n");
		move_pp = 21;
		scanf("%d", &choose);
		
		if(choose == 1)				//消息发送
		{
			M_ret();
			MOVEDOWN(16); 
			clear_down();
			M_ret();
			MOVEDOWN(16); 
			printf("开始聊天,输入exit退出该聊天\n");
			M_ret();
			MOVEDOWN(16); 
			clear_down();
			M_ret();
			MOVEDOWN(16); 
			move_pp = 16;
			while(1)
			{
				M_ret();
				MOVEDOWN(17); 
				printf("                                                          \n");
				printf(">>  ");
				scanf("%s", msg->msg_buffer);
			
				if(strcmp(msg->msg_buffer, "exit") == 0)
				{
					printf("退出当前聊天\n");
					break;
				}
				
				msg->msg_flag = msg_flag;
				
				send_size = sendto(skt_fd, msg, msg_len, 
								0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
				if(send_size == -1)
				{
					perror("发送UDP数据失败\n");
					return -1;
				}
				M_ret();
				MOVEDOWN(16); 
				printf("%s 发送成功\n", msg->msg_buffer);
				M_ret();
				MOVEDOWN(17);        
				printf("                                                                       \n");
				printf("                                                                                      \n");

			}
		}
		else if(choose == 2)       //文件发送
		{
			while(1)
			{
				M_ret();
				MOVEDOWN(16); 
				clear_down();
				move_pp = 15;
				goto_scanf();
				
				printf("当前目录如下：需要发送的文件名：%s\n", msg->fifo_name);
				system("ls");
				
				move_pp = 16;
				
				printf("\n你想发送文件吗？\n请输入文件名，否则输入exit退出该步骤\n");
				printf("输入格式为:1.jpg,\n\n");
				move_pp = 21;
				scanf("%s", s_fifo_name);
				
				if(strcmp(s_fifo_name, "exit") == 0)
				{
					M_ret();
					MOVEDOWN(16); 
					clear_down();
					M_ret();
					MOVEDOWN(16); 
					printf("退出当前聊天\n");
					move_status = 1;
					return 0;
				}
				
				//DeletS2(s_fifo_name,"send ");

				if(access(s_fifo_name, 0) == -1)
				{
					M_ret();
					MOVEDOWN(16); 
					clear_down();
					move_pp = 15;
					goto_scanf();
					printf("乱搞事情？？？ %s文件不存在\n", s_fifo_name);
					printf("重新来\n");
				}
				else
				{
					i = is_or_no_dir(s_fifo_name);

					if( i== 0)
					{
						printf("这是一个文件夹\n");

						bzero(buf, sizeof(buf));
						
						sprintf(buf,"zip -r %s.zip ./%s", s_fifo_name, s_fifo_name);
						system(buf);
						
						bzero(buf, sizeof(buf));
						sprintf(buf,"%s.zip", s_fifo_name);
						
						msg->msg_flag = fifo_send; //发送文件标志
						msg->fifo_flag = fifo_dir;
						
						msg->fifo_port = 6060;

						strcpy(s_fifo_name, buf);
						strcpy(msg->fifo_name, s_fifo_name);
						
						send_size = sendto(skt_fd, msg, msg_len, 
										0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
										
						send_fifo_init( msg, &dest_addr);
						
						bzero(buf, sizeof(buf));
						sprintf(buf,"rm %s.zip", s_fifo_name);
						system(buf);

					}
					else if(i == -1)
					{
						printf("这是一个文件\n");
						
						msg->msg_flag = fifo_send; //发送文件标志
						msg->fifo_flag = fifo_no_dir;
						
						msg->fifo_port = 6060;

						strcpy(msg->fifo_name, s_fifo_name);
						
						send_size = sendto(skt_fd, msg, msg_len, 
										0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
										
						send_fifo_init( msg, &dest_addr);
					}
					clear_down();
				}
				
				break;
			}
		}
		else if(choose == 3)
		{
			show_systems_msg();
			printf("退出当前聊天\n\n");
			goto_scanf();
			break;
		}
		else{
			 show_systems_msg();
			 printf("输入出错!\n");
			 goto_scanf();
			 break;
		}
	}
	M_ret();
	MOVEDOWN(17); 
	clear_down();
	
	move_status = 1;
	move_pp = 17;
	goto_scanf();
	return 0;
}


int show_all_friend(struct glob_info *ginfo)
{
	struct friend_list *pos;
	int status = 0;
	pos = ginfo->list_head->next->next;
	int tmp = 0;
	
	show_frient_list();
	for(pos; pos!=NULL; pos=pos->next)
	{
		if(tmp == 0)
		{
			printf("\t好友%s\t", pos->name);
			tmp++;			
		}
		else if(tmp == 1)
		{
			printf("好友%s\t", pos->name);
			tmp++;
		}
		else
		{
			printf("好友%s\t\n", pos->name);
			tmp = 0;
		}
		status = 1;
	}
	if(status == 0)
	{
		show_frient_list();
		printf("当前没有好友在线\n");
		goto_scanf();
		return 0;
	}
	
	goto_scanf();
	return 0;
}


int show_systems_msg()		//5
{
	static int s_s_m = 0;
	M_ret();
	printf("*********系统消息**********\n");
	M_ret();
	MOVEDOWN(6);
	printf("*********好友消息**********\n");
	M_ret();
	MOVEDOWN(11);
	printf("*********好友列表**********\n");
	M_ret();
	printf("*********系统消息**********\n");
	
	
 	if(s_s_m == 0)
	{
		MOVEDOWN(1);
		s_s_m++;
	}
	else if(s_s_m == 1)
	{
		MOVEDOWN(2);
		s_s_m++;
	}
	else if(s_s_m == 2)
	{
		MOVEDOWN(3);
		s_s_m++;
	}
	else if(s_s_m == 3)
	{
		printf("                                                                      \n");
		printf("                                                                       \n");
		printf("                                                                          \n");
		printf("                                                                          \n");
		M_ret();
		MOVEDOWN(2);
		s_s_m = 0;
	}
	else
	{} 
	
	


	return 0;
}


int show_frient_msg()//10
{
	static int s_s_m = 0;
	M_ret();
	MOVEDOWN(6);
	printf("*********好友消息**********\n");
	
 	if(s_s_m == 0)
	{
		MOVEDOWN(1);
		s_s_m++;
	}
	else if(s_s_m == 1)
	{
		MOVEDOWN(2);
		s_s_m++;
	}
	else if(s_s_m == 2)
	{
		printf("                                                  \n");
		printf("                                                  \n");
		printf("                                                  \n");
		M_ret();
		MOVEDOWN(7);
		s_s_m = 0;
	}
	else
	{} 

	return 0;
}

int show_frient_list()
{
	static int s_s_m = 0;
	M_ret();
	MOVEDOWN(11);
	printf("*********好友列表**********\n");
	printf("                                                  \n");
	printf("                                                  \n");
	printf("                                                  \n");
	M_ret();
	MOVEDOWN(11);
	printf("*********好友列表**********\n");
/* 	M_ret();
	MOVEDOWN(11); */
/* 	printf("*********好友列表**********\n");
 	if(s_s_m == 0)
	{
		MOVEDOWN(1);
		s_s_m++;
	}
	else if(s_s_m == 1)
	{
		MOVEDOWN(2);
		s_s_m++;
	}
	else if(s_s_m == 2)
	{
		printf("                                                  \n");
		printf("                                                  \n");
		M_ret();
		MOVEDOWN(12);
		s_s_m = 0;
	}
	else
	{}  */

	return 0;
}


int goto_scanf()//24
{
	if(move_status)
 	{
		M_ret();
		MOVEDOWN(move_pp);
		clear_down();
		M_ret();
		MOVEDOWN(move_pp);	
		printf("请输入以下选项：\n");
		printf("显示好友链表输入：  1\n");
		printf("指定好友聊天请输入：3\n");
		printf("退出请输入：        4\n");
		return 0;
	}
	
	M_ret();
	MOVEDOWN(move_pp);
	printf("                                                                   \n");
	return 0;
}


int my_readdir(char *dirname)
{
 	//1、打开目录
	DIR *dp = opendir(dirname);
	char name[32];
	if(dp == NULL)
	{
		fprintf(stderr, "opendir %s failed: %s\n", dirname, strerror(errno));
		return -1;
	}
	
	while(1)
	{
		//2、读取该目录里面的目录项
		struct dirent *p = readdir(dp);
		if(p == NULL)
		{
			printf("当前文件不存在\n");
			break;
		}
		printf("d_type: ");
		bzero(name, sizeof(name));
		
		switch(p->d_type)
		{
			case DT_BLK:
				printf("b");
				break;
				
			case DT_CHR:
				printf("c");
				break;
				
			case DT_DIR:
				printf("d\t");
				break;
				
			case DT_FIFO:
				printf("p");
				break;
				
			case DT_LNK:
				printf("l");
				break;
				
			case DT_REG:
				printf("-\t");
				break;
				
			case DT_SOCK:
				printf("s");
				break;
				
			default:
				printf("unknown");
		}
		strcpy(name, p->d_name);
		
		if(name[0] == '.')
		{
			printf("d_name2: %s \n", p->d_name);
		}
		printf("d_name1: %s \n", p->d_name);
	}
	closedir(dp);
	return 0;
}

int is_or_no_dir(char *buf)
{
	DIR *dp = opendir(buf);
	if(dp == NULL)
	{
		//fprintf(stderr, "opendir %s failed: %s\n", buf, strerror(errno));
		closedir(dp);
		return -1;
	}
	else
		closedir(dp);
		return 0;
}

int send_fifo(char *name)
{
	char buf[64];
	char b_cmd[32];

	bzero(buf, sizeof(buf));
	bzero(b_cmd, sizeof(b_cmd));
	
	my_readdir("2.jpg");
	
	int i = 0;
	while(1)
	{
			bzero(buf, sizeof(buf));
			printf("请输入\n");
			
			//gets(buf);
			
			if(strcmp(buf, "exit") == 0)
			{
				printf("退出\n");
				break;
			}
			if( strstr(buf, "send") != NULL)
			{
				printf("你想发送文件吗？请输入文件名，否则输入exit退出该步骤\n");
				printf("输入格式为:send 1.jpg,\n\n");

				if(strcmp(buf, "exit") == 0)
				{
					printf("退出\n");
					break;
				}
				
				DeletS2(buf,"send ");
				
				if(access(buf, 0) == -1)
				{
					printf("乱搞事情？？？ %s文件不存在\n", buf);
					printf("重新来\n");
				}
				else
				{
					i = is_or_no_dir(buf);
					if( i== 0)
					{
						printf("这是一个文件夹\n");
					}
					else if(i == -1)
					{
						printf("这是一个文件\n");
					}
				}
				
				
			}
			else 
			{
				//system(buf);
				printf("buf is %s\n", buf);
			}
			
	}
}

void DeletS2(char *s1,const char *s2)
{
	
    char *p=s1,*q,*c,d[80];
    unsigned long n=strlen(s2);
    
    while (1) {
        printf("%s\n",p);
        q=strstr(p,s2);
        if(q==NULL)
            break;
        c=q+n;
        *q='\0';
        strcpy(d,c);
        p=strcat(p,d);
    }
}


int clear_down()
{
	printf("                                                                           \n");
	printf("                                                                           \n");
	printf("                                                                           \n");
	printf("                                                                           \n");
	printf("                                                                           \n");
	printf("                                                                           \n");
	printf("                                                                           \n");
	printf("                                                                           \n");
	printf("                                                                           \n");

}


